package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.biz.service.ReceivingInfoService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.AdditionalCommodityDao;
import com.qdlc.p2p.dal.dao.AdditionalUserCommodityDao;
import com.qdlc.p2p.dal.dao.NewActivityAwardLogDao;
import com.qdlc.p2p.dal.dto.AdditionalCommodity;
import com.qdlc.p2p.dal.dto.AdditionalUserCommodity;
import com.qdlc.p2p.dal.dto.NewActivityAwardLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AdditionalUserCommodityModel;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;
import com.qdlc.p2p.dal.model.ReceivingInfoModel;

/**
 * Created by liaoxiangkai on 2015/12/9.
 */
@Service("giveUserCommodityService")
public class GiveUserCommodityServiceImpl implements BaseGiveAwardService {

    @Resource
    private AdditionalCommodityDao additionalCommodityDao;
    @Resource
    private AdditionalUserCommodityDao additionalUserCommodityDao;
    @Resource
    private ReceivingInfoService receivingInfoService;
    @Resource
    private NewActivityAwardLogDao newActivityAwardLogDao;

    /**
     * 规则触发赠送，
     * @param baseGiveAwardModel
     */
    @Override
    public void giveAward(BaseGiveAwardModel baseGiveAwardModel) {

    }

    /**
     * 单个用户赠送
     * @param baseGiveAwardModel
     */
    @Override
    public void giveSingleUserAward(BaseGiveAwardModel baseGiveAwardModel) {
        User user = baseGiveAwardModel.getUser();
        if (null == user) return;
        AdditionalUserCommodity additionalUserCommodity = new AdditionalUserCommodity();
        additionalUserCommodity.setUser(user);
        //查询用户收货地址
        List<ReceivingInfoModel> receivingInfoModels = receivingInfoService.ListModel(user);
        if (null == receivingInfoModels || receivingInfoModels.size() == 0) {
        } else {
        	ReceivingInfoModel receivingInfoModel =  receivingInfoModels.get(0);
            additionalUserCommodity.setReceiveAddress(receivingInfoModel.getProvince()+receivingInfoModel.getCity()+receivingInfoModel.getArea()+receivingInfoModel.getAddress());
            additionalUserCommodity.setReceivePhone(receivingInfoModel.getPhone());
            additionalUserCommodity.setReceiveMobile(receivingInfoModel.getMobile());
            additionalUserCommodity.setReceiver(receivingInfoModel.getName());
        }

        //这里ruleId只是数据传递作用，并无实在意义。即商品id
        AdditionalCommodity additionalCommodity = additionalCommodityDao.find(baseGiveAwardModel.getRuleId());
        if (null == additionalCommodity) return;
        additionalUserCommodity.setAdditionalCommodity(additionalCommodity);
        additionalUserCommodity.setStatus(AdditionalUserCommodityModel.USER_COMMODITY_AUDIT_WAIT);
        additionalUserCommodity.setSourceType(baseGiveAwardModel.getSourceType());
        additionalUserCommodity.setAddIp(Global.getIP());
        additionalUserCommodity.setDescription(baseGiveAwardModel.getDescription());
        additionalUserCommodity.setAddTime(DateUtil.getNow());
        additionalUserCommodity = additionalUserCommodityDao.save(additionalUserCommodity);
        if (null != additionalUserCommodity) {
            NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog(baseGiveAwardModel.getUser(), baseGiveAwardModel.getSourceType(), baseGiveAwardModel.getAmount(), BaseGiveAwardModel.RULE_TYPE_COMMODITY);
            newActivityAwardLogDao.save(newActivityAwardLog);
        }
    }
}
